import sqlite3
import json


def init_db():
    conn = sqlite3.connect('files.db')
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS files (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            filename BLOB NOT NULL,
            json_content TEXT NOT NULL
        )
    ''')
    conn.commit()
    conn.close()


def upload_sql_file(filename, json_file):
    conn = sqlite3.connect('files.db')
    cursor = conn.cursor()

    # 读取 JSON 文件内容
    with open(json_file, 'r') as f:
        json_content = json.load(f)

    # 将 JSON 内容转换为字符串
    json_content_str = json.dumps(json_content)

    # 插入数据到数据库
    cursor.execute('''
        INSERT INTO files (filename, json_content) VALUES (?, ?)
    ''', (filename, json_content_str))

    conn.commit()
    conn.close()



upload_sql_file("server_files/random_binary_file.bin", "server_files/random_binary_file.bin.json")


